<template>
  <div class="main">
    <div class="outDiv">
      <div class="header" v-show="!hideHeader">
        <span @click="backToPrv">
          <i class="imgIcon"></i>
        </span>
        <span class="title">升学在线</span>
      </div>
      <div class="vipArea">
        <div class="centerArea" v-if="pageStatus == 'input'">
          <div class="inputArea">
            <div class="leftLabel">卡号:</div>
            <input class="rightInput" placeholder="请输入11位卡号" v-on:blur="commonJs.blurAdjust" v-model="cardNo" maxlength="11" />
          </div>
          <div class="inputArea">
            <div class="leftLabel">密码:</div>
            <input class="rightInput" placeholder="请核对清楚填写" v-on:blur="commonJs.blurAdjust" v-model="cardPwd" />
          </div>
          <div :class="['bindBtn', isCanBind ? 'isCanBind' : '']" @click="handleComfirmBindCard">确认绑定</div>
        </div>
        <div class="centerArea" v-else-if="pageStatus == 'success'">
          <div class="status">
            <div class="statusImage"><span class="successImage"></span></div>
            <div class="statusIntro">恭喜您成功激活升学卡</div>
            <div class="statusBtn">
              <span @click="handleEnjoyService">享受权益 ></span>
            </div>
          </div>
        </div>
        <div class="centerArea" v-else-if="pageStatus == 'fail'">
          <div class="status">
            <div class="statusImage"><span class="errorImage"></span></div>
            <div class="statusIntro">您输入的密码有误，请核对后提交！</div>
            <div class="statusBtn">
              <span @click="handleBackToInput">重新提交 ></span>
            </div>
          </div>
        </div>
      </div>
      <div class="footerIntro">*此服务由升学在线APP提供  客服热线：027-88161985</div>
    </div>
  </div>  
</template>

<script>
import { backAppOrWx, checkTerminal, getUrlParam } from "~/common/common";
import { getMyVip } from "~/api/identity";
import { getCardInfo, cardsActive, cardsVip } from "~/api/flight"
import ls from 'store2'
export default {
  data() {
    return {
      cardNo: '',
      cardPwd: '',
      isCanBind: false,
      pageStatus: 'input', // input -- 输入卡密   success -- 绑定成功  fail -- 登录失败/密码错误
      hideHeader: false
    }
  },
  watch: {
    cardNo(string) {
      // this.cardPwd = ''
      this.isCanBind = false
    },
    cardPwd(string) {
      if (string == '') {
        this.isCanBind = false
      } else {
        this.isCanBind = true
      }
    }
  },
  created() {
    this.commonJs.accectAppParame();
  },
  mounted() {
    if(ls.get('userType')==3 || !ls.get('userType')){
        ls.remove('tokensyxk')
    }
    if (this.$route.query.isJustIn == 'true') {
      this.hideHeader = true
    }
    if(getUrlParam("gobackwx")){
      this.isWxPathEvent()
    }else {
      if (checkTerminal() === "ios") {
      } else if (checkTerminal() === "android") {
      } else {
        this.isWxPathEvent()
      }
    }
    this.getPrizeVipCard()
  },
  methods: {
     getPrizeVipCard() {
      if (!ls.get("userVoId")) {
        return false
      }
      getCardInfo(ls.get("userVoId")).then(res => {
          if (res.data) {
            this.cardNo = res.data[0].card_number
            this.cardPwd = res.data[0].card_password
          }
      })
    },
    backToPrv() {
      backAppOrWx()
    },
    // 确认绑定
    handleComfirmBindCard() {
      if (this.isCanBind) {
        if (this.cardNo.length !== 11) {
          this.$vux.toast.text('请输入11位卡号~');
        } else if (this.cardPwd == '') {
          this.$vux.toast.text('请输入密码~');
        } else {
          this.$loading('open')
          cardsActive({
            number: this.cardNo,
            password: this.cardPwd,
            uid: ls.get('userVoId')
          }).then(res => {
            if (res.code == '200') {
              // 绑定成功
              this.$vux.toast.text(res.msg);
              this.pageStatus = 'success'
              this.$getVip()
            } else {
              // 卡号或卡密无效
              this.$vux.toast.text(res.msg);
              this.pageStatus = 'fail'
            }
          }).finally(() => {
            this.$hideLoading()
          })
        }
      }
    },
    handleBackToInput() {
      this.$loading('open')
      setTimeout(() => {
        this.$hideLoading()
      }, 500);
      this.pageStatus = 'input'
      this.cardPwd = ''
      this.cardNo = ''
    },
    handleEnjoyService() {
      if(getUrlParam("gobackwx")){
        this.goto('../home')
      }else {
        if (checkTerminal() === "ios") {
          window.webkit.messageHandlers.backToHome.postMessage('');
        } else if (checkTerminal() === "android") {
          ZMParentsJsToAndroid.backToHome();
        } else {
          this.goto('../home')
        }
      }
    },
    goto(routerName, queryData) {
      this.$router.replace({
        path: routerName,
        query: {
          type: queryData
        }
      });
    },
    // 微信单独进入的事件
    isWxPathEvent() {
      // 判断是否登录
      if (!ls.get("userVoId")) {
        let keysPath = {
          category: "vip",
          paths: ".."+this.$route.path
        };
        sessionStorage.setItem("goesRouter", JSON.stringify(keysPath));
        this.$router.push({
          path: "../login/msgLogin"
        });
        return false;
      }
      this.getMyVip()
    },
    getMyVip() {
      if (!ls.get("userVoId")) {
        return false
      }
      this.$loading('open')
      cardsVip({ uid: ls.get("userVoId") }).then(res => {
          const isVip = res.data
          if (isVip) {
            this.$router.replace({
              path: "isVip?isJustIn=true"
            })
          }
        // }
      }).finally(() => {
        this.$hideLoading()
      })
    },
  }


}
</script>

<style lang="less" scoped>
  .outDiv {
    width: 100%;
    background: #fff;
    height: 100vh;
    .header {
      width: 100%;
      height: 0.88rem;
      line-height: 0.88rem;
      padding: 0 0.32rem;
      background: transparent;
      text-align: center;
      z-index: 100;

      .imgIcon {
        float: left;
        margin-top: 0.24rem;
        width: 0.4rem;
        height: 0.4rem;
        display:block;
        background: url('~@/static/sprite/icon_sprites_40.png') -0.1rem -0.1rem;
        background-size:3rem 2.4rem; 
      }

      .title {
        color: #222;
        font-size: 0.36rem;
        font-weight: bold;
        text-align: center;
        height: 0.88rem;
      }
    }

    .vipArea {
      width: 100%;
      background: url('https://static.shengxue985.com/sxzx_wechat/images/bg_bangding.png') no-repeat;
      background-size: 100% 5.42rem;
      padding: 2.99rem 0.32rem 0;
      .centerArea {
        width: 100%;
        height: 4.8rem;
        background: #fff;
        box-shadow: 0 0.06rem 0.24rem 0 rgba(204,204,204,0.3);
        border-radius: 0.3rem;
        padding: 0.57rem 0.32rem 0;
        .inputArea {
          margin-top: 0.15rem;
          width: 100%;
          height: 0.88rem;
          display: flex;
          justify-content: space-between;
          align-items: center;
          background: #F7F7FA;
          border-radius: 0.48rem;
          padding: 0 0.32rem;
          margin-bottom: 0.48rem;
          .leftLabel {
            color: #999;
            font-size: 0.3rem;
            line-height: 0.42rem;
            margin-right: 0.32rem;
          }
          .rightInput {
            flex: 1;
            height: 100%;
            line-height: 0.42rem;
            font-size: 0.3rem;
            color: #222;
            font-weight: bold;
            border: none;
            outline: none;
            background: transparent;
          }
          input::-webkit-input-placeholder { /* WebKit browsers */  
            color: #C7C7C7; 
            font-weight: normal;
          }  
          input:-moz-placeholder { /* Mozilla Firefox 4 to 18 */  
            color: #C7C7C7;  
            font-weight: normal;
          }  
          input::-moz-placeholder { /* Mozilla Firefox 19+ */  
            color: #C7C7C7;  
            font-weight: normal;
          }  
          input:-ms-input-placeholder { /* Internet Explorer 10+ */  
            color: #C7C7C7;  
            font-weight: normal;
          }
        }
        .inputArea:last-child { margin-top: 0; }
        .bindBtn {
          margin-top: 0.56rem;
          width: 100%;
          height: 0.88rem;
          line-height: 0.88rem;
          text-align: center;
          background: #FFAD0D;
          color: #fff;
          font-size: 0.3rem;
          font-weight: bold;
          border-radius: 0.44rem;
          opacity: 0.3;
          letter-spacing: 2px;
        }
        .isCanBind {
          opacity: 1;
        }
        .status {
          text-align: center;
          .statusImage {
            margin-bottom: 0.4rem;
            span {
              display: inline-block;
              width: 1.88rem;
              height: 1.88rem;
            }
            .successImage {
              background: url("~@/static/sprite/other_sprites.png") 0 0;
              background-size: 4.68rem 4.08rem;
            }
            .errorImage {
              background: url("~@/static/sprite/other_sprites.png") -2rem 0;
              background-size: 4.68rem 4.08rem;
            }
          }
          .statusIntro {
            color: #222;
            font-size: 0.3rem;
            line-height: 0.42rem;
            font-weight: bold;
            margin-bottom: 0.45rem;
          }
          .statusBtn {
            text-align: center;
            span {
              display: inline-block;
              color: #FFAD0D;
              font-size: 0.3rem;
              line-height: 0.42rem;
            }
          }
        }
      }
    }

    .footerIntro {
      width: 100%;
      margin-top: 3rem;
      text-align: center;
      color: #C7C7C7;
      font-size: 0.24rem;
      line-height: 0.33rem;

    }
  }
</style>